package com.cmcc.aregister.cs.database;

import com.cmcc.aregister.cs.util.UniqueKey;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DatabaseAdapter {
//	private static final String TAG = "DataBAseAdapter";
	private static final String KEY_ID = "_id";
	private static final String USERNAME = "username";
	private static final String MOBILE = "mobile";
	private static final String BIRTH = "birth";
	private static final String SEX = "sex";
	private static final String CARD = "card";
	private static final String SOCIAL_CARD = "social_card";
	private static final String LOGIN_DATE = "login_date";

	//数据库名
	private static final String DB_NAME = "mobile_database";
	//数据库用户表名
	private static final String DB_TABLE_USER = "user_info_table";

	//数据库版本
	private static final int DB_VERSION = 1;
	
	//本地Context对象
	private Context mContext = null;
	//创建用户表
	private static final String DB_CREATE_USER = "CREATE TABLE " + DB_TABLE_USER 
			+ "(" + KEY_ID + " INTEGER PRIMARY KEY," 
			+ USERNAME + " VARCHAR," 
			+ MOBILE + " VARCHAR," 
			+ BIRTH + " VARCHAR," 
			+ SEX + " INTEGER," 
			+ CARD + " VARCHAR," 
			+ SOCIAL_CARD + " VARCHAR,"
			+ LOGIN_DATE + " VARCHAR)";
	
	private SQLiteDatabase db_user = null;
	private DatabaseHelper dbHelper_user = null;

	private static class DatabaseHelper  extends SQLiteOpenHelper{

		public DatabaseHelper(Context context) {
			super(context, DB_NAME, null, DB_VERSION);
		}
		
		@Override
		public void onCreate(SQLiteDatabase db) {
			//数据库没有表时创建一个
			db.execSQL(DB_CREATE_USER);

		}

		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
			db.execSQL("DROP TABLE IF EXISTS notes");
			onCreate(db);
		}
	}
	
	/*构造函数取得Context*/
	public DatabaseAdapter(Context context){
		this.mContext = context;
	}
	
	//打开user数据库，返回数据库对象
	public void open_user() throws SQLException{
		dbHelper_user = new DatabaseHelper(mContext);
		db_user = dbHelper_user.getWritableDatabase();
	}
	
	//关闭user数据库
	public void close_user(){
		db_user.close();
		dbHelper_user.close();
	}	
	
	//插入一个用户数据
	public long insertUserData(String username,String mobile,String birth,int sex,String card,String SCard,String loginDate){
		ContentValues cv = new ContentValues();
		String sexStr = Integer.toString(sex);
		try {
			username = UniqueKey.encodeStr(username);
			mobile = UniqueKey.encodeStr(mobile);
			birth = UniqueKey.encodeStr(birth);
			sexStr = UniqueKey.encodeStr(sexStr);
			card = UniqueKey.encodeStr(card);
			SCard = UniqueKey.encodeStr(SCard);
			loginDate = UniqueKey.encodeStr(loginDate);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
		cv.put(USERNAME, username);
		cv.put(MOBILE, mobile);
		cv.put(BIRTH, birth);
		cv.put(SEX, sexStr);
		cv.put(CARD, card);
		cv.put(SOCIAL_CARD, SCard);
		cv.put(LOGIN_DATE, loginDate);
		return db_user.insert(DB_TABLE_USER, KEY_ID, cv);
	}
	//查询所有user数据
	public Cursor fetchAllUserData(){
		Cursor c = db_user.query(DB_TABLE_USER, new String[] {KEY_ID,USERNAME,MOBILE,BIRTH,SEX,CARD,SOCIAL_CARD,LOGIN_DATE}, null,null,null,null,null);
		return c;
	}
	//查询指定user数据data
	public Cursor fetchFromUser(String data) throws SQLException {
		String sql = "SELECT * FROM user_table WHERE mobile = ?";
		Cursor cursor = db_user.rawQuery(sql, new String[]{data});
		
		return cursor;
	}
	//删除除user外的所有存储上传照片周期参数的数据
	public void deleteAllUserData(){
		db_user.delete(DB_TABLE_USER, KEY_ID + ">" + 0, null);
	}
	
	//更新一条数据
	public boolean updateUserPwd(String mobile){
		ContentValues cv = new ContentValues();
		cv.put(MOBILE, mobile);
		return db_user.update(DB_TABLE_USER,cv,KEY_ID + "=" + 0,null) > 0;
	}
		
	/*//插入一条数据
	public long insertData(String data){
		ContentValues cv = new ContentValues();
		cv.put(KEY_DATA, data);
		return db_ps.insert(DB_TABLE, KEY_ID, cv);
	}
	
	//删除一条数据
	public boolean deleteData(long rowId){
//		String sql = "SELECT * FROM ps_table WHERE data = ?";
		return db_ps.delete(DB_TABLE, KEY_ID + "=" + rowId, null) > 0;
	}
	//删除所有照片数据
	public void deleteAllData(){
		db_ps.delete(DB_TABLE, KEY_ID + "<" + 999999, null);
	}
	//查询所有数据
	public Cursor fetchAllData(){
		Cursor c = db_ps.query(DB_TABLE, new String[] {KEY_ID,KEY_DATA}, null,null,null,null,null);
		if(c != null){
			c.moveToFirst();
		}
		return c;
	}
	//查询指定数据id(Long)
	public Cursor fetchData2(long rowId) throws SQLException {
		Cursor mCursor = db_ps.query(true, DB_TABLE, new String[] {KEY_ID,KEY_DATA}, KEY_ID + "=" + rowId, null, null, null,null,null);
		if(mCursor != null){
			mCursor.moveToFirst();
		}
		return mCursor;
	}
	//查询指定数据data(String)
	public String fetchData3(String data) throws SQLException {
//		Log.i("Log", "s1");
		Cursor cursor = null;
		String sql = "SELECT * FROM ps_table WHERE data = ?";
		cursor = db_ps.rawQuery(sql, new String[]{data});
//		Log.i("Log", "s2");
		String result = null;
		if (cursor.getCount() > 0) {
			cursor.moveToFirst();
			try{
				result = cursor.getString(cursor.getColumnIndex("data"));
			}catch(SQLException e){
				return null;
			}
		}
//		Log.i("Log", result);
		return result;
	}
	//查询指定数据data(Id)
	public Cursor fetchFromData(String data) throws SQLException {
		String sql = "SELECT * FROM ps_table WHERE data = ?";
		Cursor cursor = db_ps.rawQuery(sql, new String[]{data});
		
		return cursor;
	}
	//查询指定数据data(Id)
	public long fetchFromData2(String data) throws SQLException {
//		Log.i("Log", "s1");
		Cursor cursor = null;
		String sql = "SELECT * FROM ps_table WHERE data = ?";
		cursor = db_ps.rawQuery(sql, new String[]{data});
//		Log.i("Log", "s2");
		long result = 0;
		if (cursor.getCount() > 0) {
			cursor.moveToFirst();
			try{
				result = cursor.getLong(0);
			}catch(SQLException e){
				return 0;
			}
		}
		return result;
	}*/
	
	
	
}
